package cn.lhx.blog.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.GusetMessageFormBean;
import cn.lhx.blog.pojo.GuestMessage;
import cn.lhx.blog.service.GusetBookService;

public class GusetBookAction extends DispatchAction{
	@Resource
	private GusetBookService guestBookService;
	
	
	
	
	
//	
//	protected ActionForward dispatchMethod(ActionMapping mapping,
//			ActionForm form, HttpServletRequest request,
//			HttpServletResponse response, String name) throws Exception {
//		log.info("派遣方法："+name+"()");
//		if(null==name){
//			//默认调用获取留言的方法
//			return this.loadMessages(mapping, form, request, response);
//		}
//		return super.dispatchMethod(mapping, form, request, response, name);
//	}

	/**
	 * 留言
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward leaveMessage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("保存留言");
		GusetMessageFormBean gmf=(GusetMessageFormBean)form;
		String name=gmf.getName();
		String email=gmf.getEmail();
		String homeUrl=gmf.getHomeUrl();
		String umessage=gmf.getMessage();
		
		//设置值
		GuestMessage message=new GuestMessage();
		message.setName(name);
		message.setEmail(email);
		message.setHomeUrl(homeUrl);
		message.setMessage(umessage);
		message.setPostTime(new Date());
		//保存留言
		guestBookService.saveMessage(message);
		return mapping.findForward("saveOK");
	}
	
	/**
	 * 获取留言
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward loadMessages(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("获取留言");
		List<GuestMessage> messages=guestBookService.getMessages(0, 0);
		request.setAttribute("messages", messages);
		return mapping.findForward("guest_boook");
		
	}
	

}
